<template>
  <div class="yunqi">
    <!--<mt-header fixed title="小普智能">-->
    <!--<router-link to="/" slot="left">-->
    <!--<mt-button icon="back">返回</mt-button>-->
    <!--</router-link>-->
    <!--<mt-button icon="more" slot="right"></mt-button>-->
    <!--</mt-header>-->
    <div style="margin-top: 5rem">
      <span>添加小普马桶盖</span>
    </div>
    <div style="width: 60%;margin:2rem auto 0 auto">
      <img style="width: 100%" src="../../../../static/img/addto.png" />
    </div>
    <div style="margin: auto">
      <span style="font-size: 0.5rem;">连接电源开机即可连接</span>
    </div>
    <Loading v-if="showLoading" ></Loading>
    <Process v-if="showLoading" :alertText="alertText" ></Process>
    <!--<Inputk-->
    <!--v-if="showAlert"-->
    <!--:showHide="showAlert"-->
    <!--@closeTip="closeTip"-->
    <!--@goToDo="goToDo"-->
    <!--:alertText="alertText"-->
    <!--:LeftText="LeftText"-->
    <!--:RightText="RightText"-->
    <!--&gt;-->
    <!--</Inputk>-->
  </div>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
  import Loading from '@/components/common/Loading'
  import Process from '@/components/common/Process'
  import Inputk from '@/components/common/Inputk'
  import {Register} from '../../../store/_CRC'

  export default {
    name: 'Addtest',
    data () {
      return {
        showLoading:true,
        alertText:'正在努力注册设备，请稍后',
        LeftText: '',
        RightText: '',
        showAlert: true,
        BANBEN: '',
        YinJBANBEN: 'AAA111'
      }
    },
    components: {
      Loading,
      Process,
      Inputk
    },
    computed:{
      ...mapState([
      ])
    },
    watch: {
    },
    methods: {
      ...mapMutations([
        // 'getF03StringConnect'
      ]),
      tolcallback () {
        window.getStorageSync = function (key) {
          switch (key) {
            case 'deviceId':
              // return state.Duuid
              return "C8:FD:19:58:BC:10"
            // return "3C:A3:08:A9:E3:10";
            case 'serviceId':
              return "0000ffe0-0000-1000-8000-00805f9b34fb"
            case 'characteristicId':
              return "0000ffe1-0000-1000-8000-00805f9b34fb"
          }
        }
        // window.writeFunction = function (wrTore) {
        //   var deviceId = window.getStorageSync('deviceId')
        //   var serviceId = window.getStorageSync('serviceId')
        //   var characteristicId = window.getStorageSync('characteristicId')
        //   var data = window.writeTo16(wrTore)
        //   var err = window.hilink.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, data,"writeCallback")
        // }
        window.setNotify = function ()
        {
          var deviceId = window.getStorageSync('deviceId');
          var serviceId = window.getStorageSync('serviceId');
          var characteristicId = window.getStorageSync('characteristicId');
          window.hilink.notifyBLECharacteristicValueChange(deviceId,serviceId,characteristicId,true);
          console.log('执行成功:');
          setTimeout('getBenTowrite()',2000)
          return true;
        }
        window.getBenTowrite = function () {
          var deviceId = window.getStorageSync('deviceId');
          var serviceId = window.getStorageSync('serviceId');
          var characteristicId = window.getStorageSync('characteristicId');
          var wrTore = Register.getF03String(1, 78, 1)
          var data = window.writeTo16(wrTore)
          console.log(wrTore)
          var err = window.hilink.writeBLECharacteristicValue(deviceId, serviceId, characteristicId, data,"writeCallback")
        }
        window.writeTo16 = function (dataArr) {
          var newstr = '';
          for(var i = 0;i< dataArr.length;i++) {
            var str16 = (Array(2).join(0) + dataArr[i].toString(16)).slice(-2);
            newstr += str16
          }
          return newstr
        },
          window.readTo10 = function (dataStr) {
            var newArr = new Array
            for(var i = 0; i<dataStr.length;i=i+2) {
              newArr.push(parseInt(dataStr.substring(i,i+2),16))
            }
            return newArr
          },
          window.connetState = function (re)
          {
            var data = eval("("+re+")")
            if(data.connected==true) {
              console.log('连接状态:'+data.connected)
              window.hilink.onBLECharacteristicValueChange ("ValueChange")
              setTimeout("setNotify()", 1000)
            } else {
              console.log('连接失败')
            }
          },
        window.getCurrentUnregisteredDevice = function (re) {

          // var valueC = eval("("+re+")")
          // var data = valueC.data
          window.hilink.onBLEConnectionStateChange ("connetState")
          window.hilink.createBLEConnection('C8:FD:19:58:BC:10')
        }
        window.getBLESn = function () {
          return 'BBB111'
        }
      }
    },
    mounted () {
      window.writeCallback = function (re) {
        console.log(re)
      }
      window.ValueChange = function (re)
      {
        var valueC = eval("("+re+")")
        var data = valueC.data
        var dataArr = window.readTo10(data)
        console.log(dataArr)
      }
      this.tolcallback()
      // var wrTore = this.getF03StringConnect({d:1, start:58, len:1})
      window.getCurrentUnregisteredDevice('a')
      // window.hilink.getCurrentUnregisteredDevice("getCurrentUnregisteredDevice")
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .yunqi{
    text-align: center;
  }
</style>
